<?php


namespace App\Api\Services;


class ApibaiduFace
{
    /**
     * 百度请求
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    public static function requestPost($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }

    /**
     * Notes: 获取百度AccessToken
     *
     * Framer: My name is Tang
     * Date: 2021/7/20
     * Time: 10:11
     * @return mixed
     */
    public static function getBaiduAccessToken()
    {
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = 'client_credentials';
        $post_data['client_id'] = 'oeFYkMRozCBjo1HGFlDOOpo2';
        $post_data['client_secret'] = 'lheGT5xyYnB7bqpYhQjzWCe1CjhfpfrQ';
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);
        $res =  self::requestPost($url, $post_data);
        $info = json_decode($res, true);
        $accessToken = $info['access_token'];
        return $accessToken;

    }




}